package com;

import inputHelp.Utf7ImeHelper;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.os.RemoteException;

import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
import com.db.TxtHelper2;

public class CopyJiWeixin extends UiAutomatorTestCase {
	
	
	//复读主方法， 主要是获取元素，再获取语句，进行转换后发送
	//copyType： 1图灵；2直接复读
	TxtHelper2 TxtHelper2 = new TxtHelper2();
	DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	/**
	 * @param chat   qq的哪一个会话
	 * @param timeLoop  复读一次后等待时间
	 * @param serverIP  图灵服务器地址 一般是本地
	 * @param copyType  1图灵；2直接复读
	 * @param isImg  0不复读图片；1复读图片
	 * @throws RemoteException
	 * @throws UiObjectNotFoundException
	 * @throws IOException
	 */
	

	
	public void fudu(String chat1,String chat2,  String serverIP,int copyType,int isImg ,int isCopy2,int isLight) throws RemoteException,
			UiObjectNotFoundException, IOException {
		String text = "";
		String userName = "";
		String sayString = "";
		String tmpString = "";
		String tmpnoString = "";
		String notextString = "!!!no textview";
		int count = 0;
        System.out.println(sdf.format(new Date())); 
        
		while(true){
			sleep(1000);
			UiObject uio;
			UiObject uioName = null;
			UiObject uiot = null;
			count ++;
			System.out.println("$$$$$count: "+count);
			if (null==getDescUiO() || !getDescUiO().exists()) {notextString = "!!!no uio";
				if(!notextString.equals(tmpnoString)){
					System.out.println(sdf.format(new Date())+notextString);
					tmpnoString=notextString;
			}
				System.out.println("!!!! no uio");
				continue;
			}
			if(!getDescUiO().getChild(new UiSelector().className(
					"android.widget.LinearLayout").index(0)).exists()){
				if(!notextString.equals(tmpnoString)){
					System.out.println(sdf.format(new Date())+notextString);
					tmpnoString=notextString;
				}
				System.out.println("!!!! no LinearLayout");
				continue;
			}else {
				uio = getDescUiO().getChild(new UiSelector().className(
						"android.widget.LinearLayout").index(0));
			}
			if(!uio.getChild(new UiSelector().className(
							"android.widget.LinearLayout").index(1)).exists()){
				if(!notextString.equals(tmpnoString)){
					System.out.println(sdf.format(new Date())+notextString);
					tmpnoString=notextString;
			}
				System.out.println("!!!! no LinearLayout");
				continue;
			}else {
				uio = uio.getChild(new UiSelector().className(
						"android.widget.LinearLayout").index(1));
			}
				
			
			/*uio = getDescUiO().getChild(new UiSelector().className(
					"android.widget.LinearLayout").index(0)).getChild(new UiSelector().className(
							"android.widget.LinearLayout").index(1));*/
		
			if (null==uio||!uio.exists()) {
				//判断是否上次打印过
				notextString = "!!!no uio";
				if(!notextString.equals(tmpnoString)){
					System.out.println(sdf.format(new Date())+notextString);
					tmpnoString=notextString;
				}
				System.out.println("!!!! no des uio");
				continue;
			}
			//name0,1,0
			if(uio.getChild(new UiSelector().className(
									"android.widget.TextView").index(0)).exists()){
				uioName = uio.getChild(new UiSelector().className(
						"android.widget.TextView").index(0));
			}else {
				System.out.println("!!!! no name");
				continue;
			}
			if(uio.getChild(new UiSelector().className(
					"android.widget.TextView").index(1)).exists()){
				uiot = uio.getChild(new UiSelector().className(
						"android.widget.TextView").index(1));
			}else {
				System.out.println("!!!! no sayString");
				continue;
			}
			
			if(null==uiot||!uiot.exists()){
				notextString = "!!!no textview";
				if(!notextString.equals(tmpnoString)){
					System.out.println(sdf.format(new Date())+notextString);
					tmpnoString=notextString;
				}
				System.out.println("!!!! no sayString");
				continue;
			}
			
			
			sayString = uiot.getText();
			if (uioName!=null) {
				userName = uioName.getText();
			}
			text = userName+":"+sayString;
			
			
			if(text.contains("。；")){
				System.out.println("~~~~ 。； continue");
				continue;
			}
			
			text=text.replace("@", "");
			
			if(!text.equals(tmpString)){
				System.out.println("original："+text);
				tmpString=text;
				tmpnoString="";
			}
			
			
			
			//如果isSentExist已经复读过，就不复读了
			//如果isSentExist没复读，先看是不是有人复读过了，没复读过就不复读
			if(isCopy2==1){
				if(TxtHelper2.isSentExist(text)){
					notextString = "!!text exist!!";
					if(!notextString.equals(tmpnoString)){
						System.out.println(sdf.format(new Date())+notextString);
						tmpnoString=notextString;
					}
					continue;
				}else{
					if(!TxtHelper2.isExist(text)){
						TxtHelper2.insertMsg(text);
						continue;
					}else if(TxtHelper2.isUserDif(text)){
						//sendText(getCopyString(text));
						UiObject textform = new UiObject(
								new UiSelector().className("android.widget.EditText"));
						textform.clearTextField();
						textform.click();
						textform.setText(Utf7ImeHelper.e(getCopyString(text)));
						sleep(2000);
						if(! new UiObject(new UiSelector().resourceId("com.tencent.mm:id/a5k")).exists())
							continue;
						new UiObject(new UiSelector().resourceId("com.tencent.mm:id/a5k")).click();
						TxtHelper2.insertSentMsg(text);
						
					}else {
						continue;
					}
				}
			}else {
				if(TxtHelper2.isSentExist(text)){
					notextString = "!!text exist!!";
					if(!notextString.equals(tmpnoString)){
						System.out.println(sdf.format(new Date())+notextString);
						tmpnoString=notextString;
					}
					continue;
				}else{
					UiObject textform = new UiObject(
							new UiSelector().className("android.widget.EditText"));
					textform.clearTextField();
					textform.click();
					textform.setText(Utf7ImeHelper.e(getCopyString(text)));
					sleep(2000);
					if(! new UiObject(new UiSelector().resourceId("com.tencent.mm:id/a5k")).exists())
						continue;
					new UiObject(new UiSelector().resourceId("com.tencent.mm:id/a5k")).click();
					TxtHelper2.insertSentMsg(text);
				
				}
			
			}
			
		
			System.out.println("$$$copy Times:  " + count);
			count++;
			//sleep(10*1000+Math.round(Math.random()*5*1000));
		}
		
	}
	
	public static String getCopyString(String str){
		String[] sourceStrArray = str.split(":");
		return sourceStrArray[1];
	}
	
	
	
	public static UiObject getDescUiO()
			throws UiObjectNotFoundException {
		//com.tencent.mm:id/a2i
		if(new UiObject(new UiSelector().resourceId("com.tencent.mm:id/a4l")).exists())
			return getDesc(new UiObject(new UiSelector().resourceId("com.tencent.mm:id/a4l")));
		else return null;
	}
	
	
	
	
	
	public static UiObject getDesc(UiObject uio) throws UiObjectNotFoundException{
		UiObject uioDesc = null;
		for(int i =8;i>=0;i--){
			if(uio.getChild(new UiSelector().className(
					"android.widget.RelativeLayout").index(i)).exists()){
				
				uioDesc = uio.getChild(new UiSelector().className(
						"android.widget.RelativeLayout").index(i));
				break;
			}//else System.out.println(uio.getChildCount()+"uio.getChildCount()");
		}
		return uioDesc;
	}

	public static void sendText(String desct) throws UiObjectNotFoundException {
		UiObject textform = new UiObject(
				new UiSelector().className("android.widget.EditText"));
		textform.click();
		// System.out.println("###"+desct);
		textform.setText(Utf7ImeHelper.e(desct));
		//new UiObject(new UiSelector().text("发送")).click();
		//sleep(500);
		new UiObject(new UiSelector().resourceId("com.tencent.mm:id/a2c")).click();
		// sleep(50000);
	}
	
}
